var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isBottom: 0,
    is_open: 1,
    show_agreement: 0,
    pageNum:1,
    is_agree: true,
    price_index : 'none',
    navigator: { hide: 0 },
    plist:[
      { url: 'https://www.58zltc.com/images/youhui1.png', text: '拼团', jump: '/pages/shop/goods/goods?type=pin&title=拼团'},
      { url: 'https://www.58zltc.com/images/youhui2.png', text: '砍价', jump: '/pages/shop/goods/goods?type=kan&title=砍价'},
      { url: 'https://www.58zltc.com/images/youhui3.png', text: '抢购', jump: '/pages/shop/goods/goods?type=qiang&title=抢购'},
      { url: 'https://www.58zltc.com/images/youhui4.png', text: '优惠券', jump: '/pages/shop/coupon/coupon' },
      { url: 'https://www.58zltc.com/images/youhui5.png', text: '分享金', jump: '/pages/shop/goods/goods?type=qiang&title=抢购'},
    ],
  },
  toasta:function(e){
    wx.navigateTo({
      url: '/pages/commission/index',
    })
  },
  //跳转商品
  goods:function(e){
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  //跳转店铺
  shop:function(e){
    var t = this
    var type = e.currentTarget.dataset.type;
    var shop_id = e.currentTarget.dataset.id;
  
    if(type == 'takeout'){
      wx.request({
        url: 'https://www.58zltc.com/api.php/Index/getTakeoutid?token=' + t.data.token + '&openid=' + e.currentTarget.dataset.openid,
        success: res => {
          getApp().sjid = res.data.id
          wx.navigateTo({
            url: '/pages/index/takeout/info',
          })
        }
      })
    }else{
      var url = "/pages/shop/" + ((type != 'shop' && type != '' && type != 'base') ? type + '/' : '') +'detail?shop_id='+shop_id
      console.log(url)
      wx.navigateTo({
        url: url,
      })
    }
    
  },
  //特权日
  changeindex:function(e){
    var that = this
    var idx = e.currentTarget.dataset.idx;
    var dlist = this.data.dlist;
    for(var i=0;i<dlist.length;i++){
      if(i == idx){
        dlist[i].select = 1;
      }else{
        dlist[i].select = 0;
      }
    }
    this.setData({
      dlist: dlist
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Card?token=' + that.data.token + '&pageNum=' + that.data.pageNum + '&la=' + that.data.laNum + '&lo=' + that.data.loNum + '&zhou=' + dlist[idx].zhou_num + '&hao=' + dlist[idx].hao,
      success: res => {
        that.setData({
          shop_list: res.data
        })
        wx.hideLoading()
      }
    })
  },
  onReachBottom: function () {
    wx.showLoading({
      title: '加载中…',
    })
    var that = this
    var newPage = this.data.pageNum + 1;
    this.setData({
      pageNum: newPage
    })
    wx.request({
      url: 'https://www.58zltc.com/api.php/Card?token=' + this.data.token + '&pageNum=' + this.data.pageNum + '&la=' + this.data.laNum + '&lo=' + this.data.loNum + '&zhou=no',
      success: res => {
        if (that.data.shop_list.length == res.data.length) {
          that.setData({
            isBottom: 1
          })
        }
        this.setData({
          shop_list: res.data
        })
        wx.hideLoading()
      }
    })
  },
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    this.onShow();
    this.onLoad();
    setTimeout(function () {
      wx.stopPullDownRefresh()
      wx.showToast({
        title: '已刷新',
      })
    }, 2000)
  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    let that = this;
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })
    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })

    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid
    })
    const openid = app.globalData.openid;
    //时间
    app.ihttpGet("https://www.58zltc.com/api.php/Card/getdate", function (res) {
      var data = res.data;
      that.setData({
        dlist: data
      })
    });
    //优惠信息
    app.ihttpGet("https://www.58zltc.com/api.php/Card/getyhInfo", function (res) {
      that.setData({
        member_count: res.data.member_count,
        shop_count: res.data.shop_count
      })
    })
    //店铺
    wx.getLocation({
      type: 'wgs84',
      complete: function (resq) {
        that.setData({
          laNum: resq.latitude,
          loNum: resq.longitude
        })
        wx.request({
          url: 'https://www.58zltc.com/api.php/Card?token=' + that.data.token + '&pageNum=' + that.data.pageNum + '&la=' + that.data.laNum + '&lo=' + that.data.loNum + '&zhou=no',
          success: res => {
            that.setData({
              shop_list: res.data
            })
            wx.hideLoading()
            if (that.data.shop_list) {
              that.getshop()
            }
          }
        })
      }
    })
   
    
  },
  backto: function () {
    wx.navigateBack({});
  },
  //获取下面店铺
  getshop:function(){
    var that =this
    wx.request({
      url: 'https://www.58zltc.com/api.php/Card/getshop?token=' + that.data.token ,
      success: res => {
        that.setData({
          you:res.data
        })
        wx.hideLoading()
      }
    })
  }
})